clearvars -except Globaloption option

if exist('Globaloption', 'var') == 0
    Globaloption.savefig = 1;
end

option.robust = 4;

%% plot colony adjusted
starttime = 1729;
endtime = 1946;

ttime = [starttime:1:endtime];

filename = ['../before1946/MAT/cy_UK_case', num2str(option.robust)];
load (filename);
UKmat = xlsread('../../data/UK_colony/colonial_debt_cleaned.xls', 'UK');
INmat = xlsread('../../data/UK_colony/colonial_debt_cleaned.xls', 'IN');
SAmat = xlsread('../../data/UK_colony/colonial_debt_cleaned.xls', 'SA');
AUmat = xlsread('../../data/UK_colony/colonial_debt_cleaned.xls', 'AU');
CAmat = xlsread('../../data/UK_colony/colonial_debt_cleaned.xls', 'CA');
NZmat = xlsread('../../data/UK_colony/colonial_debt_cleaned.xls', 'NZ');

f = figure;
plot(SAmat(:, 1), SAmat(:, 2) ./ SAmat(:, 3), 'LineWidth', 2)
hold on
plot(AUmat(:, 1), AUmat(:, 2) ./ AUmat(:, 3), 'LineWidth', 2)
hold on
plot(CAmat(:, 1), CAmat(:, 2) ./ CAmat(:, 3), 'LineWidth', 2)
hold on
plot(INmat(:, 1), INmat(:, 2) ./ INmat(:, 3), 'LineWidth', 2)
hold on
plot(NZmat(:, 1), NZmat(:, 2) ./ NZmat(:, 3), 'LineWidth', 2)
hold on
plot(SAmat(:, 1), gdebt(1:find(SAmat(:, 1) == 1945)), 'black', 'LineWidth', 2.5)
ylim([0 2])

xlabel('Year')
ylabel('Ratio to UK GDP')

grid on
legend('South Africa', 'Australia', 'Canada', 'India', 'New Zealand', 'UK', 'location', 'northwest')
f.PaperSize = [6.5 4];
if Globaloption.savefig == 1
    print('../../results/figures_main/UK/gdebt_colonies', '-dpng', '-r1000')
end

%% plot colony surplus
data_colony_surplus = readtable('../../data/UK_colony/colony_surplus.xlsx');
date2 = data_colony_surplus.year;
rev_colony = data_colony_surplus.tau;
spending_colony = data_colony_surplus.g;
surplus_colony = rev_colony - spending_colony;

f = figure;
f.Position = [1000 918 560 300];
box on;
ha = plot(date2, 100 * surplus_colony, 'black', 'LineWidth', 3);
hold on
hb = plot(date2, 100 * rev_colony, 'red--', 'LineWidth', 3);
hc = plot(date2, 100 * spending_colony, 'blue-.', 'LineWidth', 3);
hleglines = [ha hb hc];
hleg = legend(hleglines, 'Surplus/GDP', 'Tax/GDP', 'Spend/GDP');
ylabel('Ratio to UK GDP')
set(gca, 'Layer', 'top', 'FontSize', 9)
set(gca, 'FontName', 'Times New Roman')

xlabel('Year')
ylabel('Ratio to UK GDP')


f.PaperSize = [6 3.75];
grid on

if Globaloption.savefig == 1
    print('../../results/figures_main/UK/Colony/UK_colonysurpluses', '-dpng')
end

%
samplestart = min(date2);
sampleend = max(date2);
rev_colony = [nan(samplestart - 1729, 1); rev_colony; nan(1946 - sampleend, 1)];
spending_colony = [nan(samplestart - 1729, 1); spending_colony; nan(1946 - sampleend, 1)];

s_colony = exp(pdT) .* (taxrevgdp + rev_colony)' - exp(pdG) .* (spendgdp + spending_colony)';

samplestart = 1729;
startdatenum = samplestart;

%% read gfd data for india
year = [1729:1:1946]';
data_UK = xlsread('../../data/UK_colony/colonial_debt_cleaned.xls', 'UK');

INGDP = INmat(1:end, 3);
UKGDP = data_UK(:, 2);

%% India government finance data
Indiasurplusdata = readtable('../../data/UK_colony/India.xlsx');
samplestart = 1839;
sampleend = 1919;

India_Revenue = Indiasurplusdata.GROSSREVENUE_;
India_Spending = Indiasurplusdata.GROSSEXPENDITURE_;
India_Revenue = [nan(samplestart - 1729, 1); India_Revenue; nan(1945 - sampleend, 1)];
India_Spending = [nan(samplestart - 1729, 1); India_Spending; nan(1945 - sampleend, 1)];

%% India Surplus
India_Revenue_UKGDP = India_Revenue ./ UKGDP / 1000000;
India_Spending_UKGDP = India_Spending ./ UKGDP / 1000000;
India_Surplus_UKGDP = India_Revenue_UKGDP - India_Spending_UKGDP;
date2 = 1729:1945';

f = figure;
f.Position = [1000 918 560 300];
box on;
ha = plot(date2, 100 * India_Surplus_UKGDP, 'black', 'LineWidth', 3);
hold on
hb = plot(date2, 100 * India_Revenue_UKGDP, 'red--', 'LineWidth', 3);
hc = plot(date2, 100 * India_Spending_UKGDP, 'blue-.', 'LineWidth', 3);
hleglines = [ha hb hc];
hleg = legend(hleglines, 'Surplus/GDP', 'Tax/GDP', 'Spend/GDP');
ylabel('% of UK GDP')
set(gca, 'Layer', 'top', 'FontSize', 9)
set(gca, 'FontName', 'Times New Roman')

xlabel('Year')
ylabel('Ratio to UK GDP')

f.PaperSize = [6 3.75];
grid on

if Globaloption.savefig == 1
    print('../../results/figures_main/UK/Colony/India_Surplus', '-dpng', '-r1000')
end

%% Use actual India Number
colony_debt_gdp = (SAmat(:, 2) + AUmat(:, 2) +CAmat(:, 2) + INmat(:, 2) + NZmat(:, 2)) ./ UKmat(:, 2);
colony_debt_gdp = [colony_debt_gdp; nan];
colony_debt_gdp(1:1850 - 1729 + 1) = nan;

s_colony_india3 = exp(pdT) .* (taxrevgdp)' + exp(pxbar) .* (rev_colony + fillmissing([India_Revenue_UKGDP; nan], 'constant', 0))' - exp(pdG) .* (spendgdp)' - exp(pxbar) .* (spending_colony + fillmissing([India_Spending_UKGDP; nan], 'constant', 0))';

samplestart = 1729;

%% campbell shiller post1850
load ('MAT/colonyresult_post1850.mat');

% subsample 1850-1914
samplestart = 1850;
startdatenum = samplestart;
statetime = find(ttime == samplestart);

f = figure;
f.Position = [1000 918 560 300];
hg = plot(ttime(1 + samplestart - startdatenum:end), gdebt(2 + samplestart - startdatenum:end)', 'Color', 'blue', 'LineWidth', 2.5); hold on;
ha = plot(ttime(1 + samplestart - startdatenum:end), gdebt(2 + samplestart - startdatenum:end)' + colony_debt_gdp(1 + samplestart - startdatenum:end)', 'Color', 'green', 'LineWidth', 2.5, 'LineStyle', '--'); hold on;
hc = plot(ttime(1 + samplestart - startdatenum:end), s, 'Color', 'r', 'LineWidth', 2.5); hold on;
he = plot(ttime(1 + samplestart - startdatenum:end), [nan(1, 1850 - 1729), s_withcolony], 'Color', 'black', 'LineWidth', 2.5, 'LineStyle', '--'); hold on;

hleglines = [hg ha hc he];
set(gca, 'Layer', 'top', 'FontSize', 9)
set(gca, 'FontName', 'Times New Roman')
grid on
axis([samplestart max(ttime) -0.5 2.5])
legend(hleglines, 'Debt/GDP Benchmark', 'Debt/GDP With Colonies', 'FB Benchmark', 'FB with Colonies', 'location', 'northwest')
set(gca, 'Layer', 'top', 'FontSize', 9)
set(gca, 'FontName', 'Times New Roman')
f.PaperSize = [6 3.75];

xlabel('Year')
ylabel('PV(Surplus)/GDP')

if Globaloption.savefig == 1
    print('../../results/figures_main/UK/Colony/colony_debt_surplus_subsample_new.png', '-dpng')
end

%% Handles
fID = fopen('../../results/tables_main/UK/Table_UKcolony_sumstats.tex', 'w');

mean(s_colony_india3(find(SAmat(:, 1) == 1860):find(SAmat(:, 1) == 1919)))
totaldebt = gdebt(2 + samplestart - startdatenum:end)' + colony_debt_gdp(1 + samplestart - startdatenum:end)';
nanmean(totaldebt(find(SAmat(:, 1) == 1860):find(SAmat(:, 1) == 1919)))

fprintf(fID, '\\newcommand{\\colonysamplesurplus}{%4.2f}\n', 100 * mean(s_withcolony(2:70)));
fprintf(fID, '\\newcommand{\\colonysampledebt}{%4.2f}\n', 100 * nanmean(totaldebt(find(SAmat(:, 1) == 1860):find(SAmat(:, 1) == 1919))));
fprintf(fID, '\\newcommand{\\colonysamplesurplusbenchmark}{%4.2f}\n', 100 * mean(s(find(SAmat(:, 1) == 1860):find(SAmat(:, 1) == 1919))));
fprintf(fID, '\\newcommand{\\colonysampledebtbenchmark}{%4.2f}\n', 100 * mean(gdebt(find(SAmat(:, 1) == 1860):find(SAmat(:, 1) == 1919))));
fprintf(fID, '\\newcommand{\\colonysamplediff}{%4.2f}\n', 100 * mean(s(find(SAmat(:, 1) == 1860):find(SAmat(:, 1) == 1919))) - 100 * mean(gdebt(find(SAmat(:, 1) == 1860):find(SAmat(:, 1) == 1919))));

fprintf(fID, '\\newcommand{\\withoutcolonysdebtratio}{%4.2f}\n', 100 * nanmean(s(1 + samplestart - 1729:end)) ./ nanmean(gdebt(2 + samplestart - 1729:end)'));
fprintf(fID, '\\newcommand{\\withcolonysdebtratio}{%4.2f}\n', 100 * nanmean(s_withcolony(1:end)) ./ (nanmean(gdebt(2 + samplestart - 1729:end) + nanmean(colony_debt_gdp(1 + samplestart - 1729:end)))));

%%Close File
fclose(fID);
